package cn.swust.designpatternlearningplatform.service.impl;

import cn.swust.designpatternlearningplatform.listener.DemoDataListener;
import cn.swust.designpatternlearningplatform.dao.ClassManagementDao;
import cn.swust.designpatternlearningplatform.dao.StudentDao;
import cn.swust.designpatternlearningplatform.dao.TeacherDao;
import cn.swust.designpatternlearningplatform.domain.dto.ClassDto;
import cn.swust.designpatternlearningplatform.domain.bo.StudentSimplyInformationBo;
import cn.swust.designpatternlearningplatform.domain.dto.StudentSimplyInformationDto;
import cn.swust.designpatternlearningplatform.domain.vo.StudentVo;
import cn.swust.designpatternlearningplatform.service.ClassManagementService;
import com.alibaba.excel.EasyExcel;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;


import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@Transactional
@Slf4j
public class ClassManagementServiceImpl implements ClassManagementService {
    @Resource
    private ClassManagementDao classManagementDao;
    @Resource
    private StudentDao studentDao;
    @Resource
    private TeacherDao teacherDao;

    @Override
    public Set<String> getSemester(String teacherAccount) {
       Set<String> list=classManagementDao.getSemester(teacherAccount);
        return list;
    }

    @Override
    public List<ClassDto> getClassNumber(String semester,String teacherAccount) {
        List<ClassDto> list=new ArrayList<>();
        List<String> classNumber=classManagementDao.getClassNUmber(semester,teacherAccount);
        ClassDto classDto=new ClassDto();
        classDto.setStudentNumber(classNumber.size());
        classDto.setClassNumber("所有班级");
        list.add(classDto);
        Map<String, Long> collect = classNumber.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
        for(Map.Entry<String,Long> map:collect.entrySet())
        {
            ClassDto classDto1=new ClassDto();
            classDto1.setClassNumber(map.getKey());
            classDto1.setStudentNumber(Math.toIntExact(map.getValue()));
            list.add(classDto1);
        }
        return list;
    }

    @Override
    public StudentSimplyInformationDto getStudentInformation(String classNumber, Integer currentPage, Integer pageSize, String semester, String teacherAccount) {
        StudentSimplyInformationDto studentSimplyInformationDto=new StudentSimplyInformationDto();
        List<StudentSimplyInformationBo> list=new ArrayList<>();
        List<StudentSimplyInformationBo> pageList=new ArrayList<>();
        if(!classNumber.isEmpty())
        {
            list=classManagementDao.getList(classNumber,semester,teacherAccount);
        }
        else {
             list=classManagementDao.getAllList(semester,teacherAccount);

        }
        int count = 0;
        if(list != null && list.size() > 0) {
            count = list.size();
            int fromIndex = (currentPage -1)* pageSize;
            int toIndex = currentPage  * pageSize;
            if (toIndex > count) {
                toIndex = count;
            }
            if(fromIndex>count)
            {
               pageList=null;
            }
            else{
                pageList = list.subList(fromIndex, toIndex);
            }
            studentSimplyInformationDto.setStudentSimplyInformationBo(pageList);
            studentSimplyInformationDto.setTotal(count);

        }
       return studentSimplyInformationDto;

    }

//    @Override
//    public void BuildClass(String teacherAccount, String classNumber, String semester) {
//        Teacher teacher=teacherDao.getTeacherByNumber(teacherAccount);
//        classManagementDao.BuildClass(teacher.getTeacherName(),teacherAccount,classNumber,semester);
//    }

    @Override
    public void AddStudent(MultipartFile file,String teacherAccount,String semester) {
        try {
            EasyExcel.read(file.getInputStream(), StudentVo.class,new DemoDataListener(studentDao,classManagementDao,teacherAccount,semester)).sheet().doRead();
        } catch (Exception e) {
           log.info(String.valueOf(e));
        }
    }

    @Override
    public void DeleteClass(String semester, String classNumber,String teacherAccount) {
        classManagementDao.DeleteClass(semester,classNumber,teacherAccount);
    }

    @Override
    public void DeleteStudents(List<String> studentNumbers) {
        studentNumbers.forEach(s->{
            classManagementDao.DeleteStudent(s);
        });
    }
}
